package Poker;

import java.util.Collections;
import java.util.Vector;

//Pokazuje graczowi jakie ma karty, zarz�dza graczem
public class Account {
	private Vector<Card> cards = new Vector<Card>(); //Przechowuje list� wylosowanych kart 0 - najwyzsza 5- najnizsza
	private Player player;
	private int chips = 0; //Ile gracz ma �eton�w
	private int chips_in_game = 0; //Ile gracz postawi�
	
	public Account()
	{
		 
	}
	
	public Account(String player_name, String player_type) 
	{
		if(player_type.equals("human"))
		{
			player = new Human(player_name);
		}
		else if(player_type.equals("bot"))
		{
			player = new Bot(player_name);
		}
	}
	
	public String GetName()
	{
		return player.GetName();
	}
	
	public int GetChips()
	{
		return chips;
	}
	
	public Vector<Card> GetCards()
	{
		return cards;
	}
	
	public void SetCards(Vector<Card> cards)
	{
		this.cards = cards;
		CardComparator cmp = new CardComparator();
		Collections.sort(this.cards,cmp);
		player.SetCards(this.cards);
	}
	public void PrintChangeCard()
	{
		player.PrintChange();
	}
	public void Message(String message)
	{
		player.Message(message);
	}
	
	public int[]  GetScore()
	{
		/* Brak zaimplementowania sprawdzania "wy�szej" karty */
		int[] score = new int[2];
		if(cards.isEmpty())
		{
			score[0] = 0;
			score[1] = 0;
			return score;
		}
		
		if(is_straight()) // Strit
		{
			if(same_color())
			{
				if(cards.elementAt(0).GetFigure() == 12) //Taki sam kolor
				{
					score[0] = 12;
					score[1] = 0; //kpoker	
				}
				else //poker
				{
					score[0] = 11;
					score[1] = 0;
				}	
			}
			else
			{
				score[0] = 7;
				score[1] = 0; //strit
			}
		}
		else if(is_quad())
		{
			score[0] = 10;
			score[1] = 0;
		}
		else if(is_triple() && (is_pair(0) || is_pair(3)))
		{
			score[0] = 9;
			score[1] = 0;
		}
		else if(same_color())
		{
			score[0] = 8;
			score[1] = 0;
		}
		else if(is_triple())
		{
			score[0] = 6;
			score[1] = 0;
		}
		else if(is_double_pair())
		{
			score[0] = 5;
		}
		else if(is_pair(0) ||is_pair(1) || is_pair(2) || is_pair(3) )
		{
			score[0] = 4;
		}
		else
		{
			score[0] = 0;
			score[1] = cards.elementAt(0).GetFigure();
		}
		return score;
	}
	
	public boolean Entry(int entry)
	{
		if(player.Entry(entry))
		{
			chips -= entry;
			chips_in_game = entry;
			player.SetChips(chips);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public int Bet(int bet)
	{
		int b = 0;
		while(true) //Gracz b�dzie pytany a� odpowie dobrze
		{
			b = player.Bet(bet);
			if(b == chips) //Gra vabank
			{
				chips_in_game = chips;
				player.SetChipsInGame(chips_in_game);
				return b;
			}
			else if(b > chips) //Nie masz tyle �eton�w
			{
				player.Message("Nie masz tylu �eton�w!");
			}
			else if(b < bet && b > -1) //Obstawi� za ma�o
			{
				player.Message("Zak�ad jest zbyt niski!");
			}
			else if(b > bet) //Podbi� stawk�
			{
				chips_in_game = b;
				player.SetChipsInGame(chips_in_game);
				return b;
			}
			else if(b == -1) //Zrezygnowa� z gry
			{
				return -1;
			}
			else //Nic nie zmieni�
			{
				return b;
			}
		}
	}
	
	public Vector<Integer> Query()
	{
		return player.Query();
	}
	
	private boolean same_color()
	{
		if ((cards.elementAt(0).GetColor() == 
				(cards.elementAt(1).GetColor())) &&
				(cards.elementAt(0).GetColor() == 
				(cards.elementAt(2).GetColor())) &&
				(cards.elementAt(0).GetColor() == 
				(cards.elementAt(3).GetColor())) &&
				(cards.elementAt(0).GetColor() == 
				(cards.elementAt(4).GetColor())))
			return true;
		else
			return false;
	
	}
	
	private boolean is_straight()
	{
		if((cards.elementAt(0).GetFigure() == 
				(cards.elementAt(1).GetFigure()+1)) &&
				(cards.elementAt(0).GetFigure() == 
				(cards.elementAt(2).GetFigure()+2)) &&
				(cards.elementAt(0).GetFigure() == 
				(cards.elementAt(3).GetFigure()+3)) &&
				(cards.elementAt(0).GetFigure() == 
				(cards.elementAt(4).GetFigure()+4)))
			return true;
		else
			return false;
	}
	
	private boolean is_quad()
	{
		if((is_pair(0) && is_pair(1) && is_pair(2)) ||(is_pair(1) && is_pair(2) && is_pair(3))) 
			return true;
		else
			return false;
	}
	
	private boolean is_triple()
	{
		if((is_pair(0) && is_pair(1)) || (is_pair(1) && is_pair(2)) || (is_pair(2) && is_pair(3))) 
			return true;
		else
			return false;
	}
	
	private boolean is_double_pair()
	{
		if ((is_pair(0) && is_pair(2)) || (is_pair(0) && is_pair(3)) || (is_pair(1) && is_pair(3)))
			return true;
		else
			return false;
	}

	private boolean is_pair(int pos)
	{
		if ((cards.elementAt(pos).GetFigure() == (cards.elementAt(pos+1).GetFigure())))
			return true;
		else
			return false;
	}

	public void SetChips(int chips)
	{
		player.SetChips(chips);
		player.SetChipsInGame(chips_in_game);
		this.chips = chips;
	}
	
	public void SetChipsInGame(int chips_in_game)
	{
		player.SetChips(chips);
		player.SetChipsInGame(chips_in_game);
		this.chips_in_game = chips_in_game;
	}

	public int GetChipsInGame()
	{
		return chips_in_game;
	}
}
